/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.subcomponents;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.model.ConfigurationModel;
import com.elf.gwt.common.client.model.ModuleModel;
import com.elf.gwt.common.client.model.ValueModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 *  
 */
public class ConfigurationSubComponent extends AdminSubComponent
{
	protected ModuleModel moduleModel;
	protected Grid grid;
	protected List<Widget> editWidgets;
	protected HorizontalPanel buttonPanel = null;
	protected Button removeButton;
	protected Button editButton;
	protected Button installButton;
	protected Button cancelButton;
	protected Button updateButton;

	protected boolean edit = false;
	protected String action = "";

	/**
	 * Constructor
	 */
	public ConfigurationSubComponent(ModuleModel moduleModel, AdminComponentController controller, String handle, SubComponentCallback callback)
	{
		super(controller, handle, null, callback);
		this.moduleModel = moduleModel;
		editWidgets = new ArrayList<Widget>();
	}

	public void build()
	{
		action = "";
		List<ConfigurationModel> configurationModels = moduleModel.getConfigurations();
		int sz = configurationModels.size();

		int rows = (sz * 3) + 2;
		int col = 1;
		grid = new Grid(rows, col);
		grid.setText(0, 0, moduleModel.getCode());

		int j = 0;
		for (int i = 2; i < rows; i++)
		{
			ConfigurationModel configurationModel = configurationModels.get(j);
			grid.getRowFormatter().addStyleName(i, "Title");
			grid.setText(i, 0, configurationModel.getTitle());
			i++;

			String value = configurationModel.getValue();
			if (edit)
			{
				Widget widget = getEditWidget(configurationModel, value);
				editWidgets.add(widget);
				grid.setWidget(i, 0, widget);
			}
			else
			{
				String displayValue = getDisplayValue(configurationModel, value);
				grid.setText(i, 0, displayValue);
			}
			i++;
			grid.setHTML(i, 0, "&nbsp;");
			j++;
		}

		if (buttonPanel == null)
		{
			buttonPanel = new HorizontalPanel();
			buttonPanel.setStyleName("ZButtons");

			removeButton = new Button("remove", removeHandler);
			removeButton.addStyleName("ComponentLink");
			buttonPanel.add(removeButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

			editButton = new Button("edit", editHandler);
			editButton.addStyleName("ComponentLink");
			buttonPanel.add(editButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

			installButton = new Button("install", installHandler);
			installButton.addStyleName("ComponentLink");
			buttonPanel.add(installButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));
			
			cancelButton = new Button("cancel", cancelHandler);
			cancelButton.addStyleName("ComponentLink");
			buttonPanel.add(cancelButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));
			
			updateButton = new Button("update", updateHandler);
			updateButton.addStyleName("ComponentLink");
			buttonPanel.add(updateButton);
		}
		removeButton.setVisible(moduleModel.isInstalled() && !edit);
		editButton.setVisible(moduleModel.isInstalled() && !edit);
		installButton.setVisible(!moduleModel.isInstalled() && !edit);
		cancelButton.setVisible(edit);
		updateButton.setVisible(edit);
		
		clear();
		add(new InlineHTML("<br/><br/><br/>"));
		add(buttonPanel);
		add(grid);

		if (edit) {
		}
	}

	protected String getDisplayValue(ConfigurationModel configurationModel, String value)
	{
		String displayValue = value;

		List<ValueModel> values = configurationModel.getValues();
		if (values != null && values.size() > 0) {
			for (Iterator<ValueModel> iter = values.iterator(); iter.hasNext(); )
			{
				ValueModel valueModel = iter.next();
				if (value.equals(valueModel.getValue())) displayValue = valueModel.getTitle();
			}
		}
		return displayValue;
	}

	protected Widget getEditWidget(ConfigurationModel configurationModel, String value)
	{
		Widget editValue = null;
		String format = configurationModel.getFormat();
		List<ValueModel> values = configurationModel.getValues();
		if (values != null && values.size() > 0) {
			ListBox listBox = null;
			String[] selectedValues = null;
			if (format.toLowerCase().equals("multi_select")) {
				listBox = new ListBox(true);
				selectedValues = value.split(",");
				for (int i=0; i< selectedValues.length; i++)
				{
					selectedValues[i] = selectedValues[i].trim();
				}
			}
			else {
				listBox = new ListBox();
			}

			int i=0;
			listBox.clear();
			for (Iterator<ValueModel> iter = values.iterator(); iter.hasNext(); )
			{
				ValueModel valueModel = iter.next();
				listBox.addItem(valueModel.getTitle(),valueModel.getValue());
				listBox.setItemSelected(i, false);
				
				if (listBox.isMultipleSelect()) {
					for (int j=0; j< selectedValues.length; j++)
					{
						if (selectedValues[j].equals(valueModel.getValue())) {
							listBox.setItemSelected(i, true);
							break;
						}
					}					
				}
				else {
					if (value.equals(valueModel.getValue())) {
						listBox.setItemSelected(i, true);
					}
				}
				i++;
			}
			editValue = listBox;
		}
		else if (format != null && format.equalsIgnoreCase("textarea")) {
			TextArea textArea = new TextArea();
			textArea.setText(value);
			textArea.setCharacterWidth(40);
			textArea.setVisibleLines(10);
			editValue = textArea;
		}
		else {
			TextBox textBox = new TextBox();
			textBox.setText(value);
			editValue = textBox;
		}
		return editValue;
	}

	protected ClickHandler editHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			edit = true;
			build();
		}
	};

	protected ClickHandler cancelHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			edit = false;
			build();
		}
	};

	protected ClickHandler updateHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "Update";
			callback.onSuccess(thisComponent);
		}
	};

	protected ClickHandler installHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "Install";
			callback.onSuccess(thisComponent);
		}
	};

	protected ClickHandler removeHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = "Remove";
			callback.onSuccess(thisComponent);
		}
	};

	public void setEdit(boolean edit) {
		this.edit = edit;
	}

	public String getAction()
	{
		return action;
	}

	public List<Widget> getEditWidgets() {
		return editWidgets;
	}
}
